package com.bds.portal.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bds.model.Project;
import com.bds.model.Result;
import com.bds.portal.common.result.Page;
import com.bds.portal.common.result.USession;
import com.bds.portal.service.ProjectService;
import com.bds.portal.util.Const;

@Controller
@RequestMapping("/action/my/p")
public class ProjectAction extends BaseAction {
	private static Logger logger = Logger.getLogger(ProjectAction.class);

	@Resource
	private ProjectService projectService;

	@RequestMapping(value = "/toProject")
	public ModelAndView toCompProject(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView view = new ModelAndView();
		try {

			view.setViewName("/web/my/project/project_list.jsp");

		} catch (Exception e) {
			logger.error("", e);
			return null;
		}
		return view;
	}

	@RequestMapping(value = "/getProjectList")
	@ResponseBody
	public Page getCompProject(HttpServletRequest request, HttpServletResponse response, Integer pageNum,
			Integer pageSize) throws Exception {
		Page page = new Page();
		try {
			USession us = this.getUserSession(request);
			page = projectService.getProjectList(us.getId(), pageNum, pageSize);
		} catch (Exception e) {
			page.setCode(Const.FAIL_500);
			logger.error("", e);
		}
		return page;
	}

	@RequestMapping(value = "/saveProject")
	@ResponseBody
	public Result saveProject(HttpServletRequest request, HttpServletResponse response, String pname,
			String projectDesc) throws Exception {
		Result ret = new Result();
		try {
			USession us = this.getUserSession(request);
			if (us == null) {
				ret.setResult(Const.FAIL, "请重新登陆");
				return ret;
			}

			if (projectService.getProjectByName(us.getId(), pname) == null) {
				projectService.saveProject(us.getId(), pname, projectDesc);
				List<Project> plist = projectService.getProjectList(us.getId());
				us.setPlist(plist);
				ret.setResult(Const.SUC, "操作成功");
			} else {
				ret.setResult(Const.FAIL, "项目名称重复");

			}

		} catch (Exception e) {
			ret.setCode(Const.FAIL_500);
			logger.error("", e);
		}
		return ret;
	}

}
